package com.weirong.smartvault.backservice.util.table;

import com.weirong.smartvault.backservice.entity_model.*;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: kfeb4
 * Date: 2017/12/30
 * Time: 13:01
 */
public class CommandTable {

    public static Table<CommandModel> buildTable(ForeignCurrencyRequestModel request) throws InvocationTargetException, NoSuchMethodException, NoSuchFieldException, InstantiationException, IllegalAccessException {
        Table<CommandModel> table = new Table<>(7);
        Collection<MoneyLogCollectionModel<MoneyLogModel>> t = request.toMoneyLogCollection();
        List<MoneyLogCollectionModel<MoneyLogModel>> money = new ArrayList<>(t);
        List<CommandModel> commands = request.getAllCommands();//包

        Integer max = money.size() > commands.size() ? money.size() : commands.size();

        for (int i = 0; i < max; i++) {
            setRequests(table, money, i);
            if(i<commands.size()){
                //填写包信息
                CommandModel commandModel = commands.get(i);
                List<MoneyLogModel> deliverys = commandModel.getCommandsDetail();
                table.setOne(3,"指令"+(i+1),deliverys.size());
                for(MoneyLogModel moneyLogModel:deliverys){
                    table.setOne(4,moneyLogModel.getCurrencyName(),1);
                    table.setOne(5, String.valueOf(moneyLogModel.getDenominationType()),1);
                    table.setOne(6, String.valueOf(moneyLogModel.getAmount()),1);
                }
            }
        }
        table.freeze();
        return table;
    }

    static void setRequests(Table<CommandModel> table, List<MoneyLogCollectionModel<MoneyLogModel>> money, int i) throws NoSuchFieldException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        if(i<money.size()){
            MoneyLogCollectionModel<MoneyLogModel> collectionModel = money.get(i);
            table.setOne(0, collectionModel.getCurrencyName(), collectionModel.getSize());
            for (MoneyLogModel moneyLogModel : collectionModel.getLogs()) {
                table.setOne(1, String.valueOf(moneyLogModel.getDenominationType()), 1);
                table.setOne(2, String.valueOf(moneyLogModel.getAmount()), 1);
            }
        }
    }

    public static Table<CommandModel> buildRequestTable(List<ForeignCurrencyRequestModel> requests) throws NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
        Table<CommandModel> table = new Table<>(7);
        for (int i = 0; i < requests.size(); i++) {
            ForeignCurrencyRequestModel model = requests.get(i);
            List<CommandModel> commands = model.getAllCommands();
            table.setOne(0, String.valueOf(i),commands.size());
            table.setOne(1,model.getDepartmentName(),commands.size());
            for(CommandModel commandModel:commands) {
                table.setOne(2,commandModel.getName(),1);
                table.setOne(3,commandModel.getCreateTime(),1);
                table.setOne(4,commandModel.getSendedTime(),1);
                table.setOne(5,commandModel.getSendResult(),1);
            }
            table.setOne(6, String.valueOf(i),commands.size());
        }
        return table;
    }

}
